package smartcarpool.smartspaceservice;

import java.util.Vector;

import android.app.PendingIntent;
import sofia_kp.KPICore;

import android.util.Log;


public class SmartSpaceInsert{// implements Runnable {
	
	private KPICore smartSpace;
	private String HOST;
	private int PORT;
	private String SPACE_NAME;
	private Vector<Vector<String>> tripleVector;
    private PendingIntent pendingIntent;
    private SS_KP kp;
	
	public SmartSpaceInsert(String HOST, int PORT, String SPACE_NAME, Vector<Vector<String>> tripleVectorToInsert, PendingIntent pendingIntent) {
		this.HOST = HOST;
		this.PORT = PORT;
		this.SPACE_NAME = SPACE_NAME;
		this.tripleVector = tripleVectorToInsert;
        this.pendingIntent = pendingIntent;
	}

    public SmartSpaceInsert(SS_KP kp) {
        this.HOST = HOST;
        this.PORT = PORT;
        this.SPACE_NAME = SPACE_NAME;
        this.kp = kp;
        this.smartSpace = kp.getKPICore();
        //this.tripleVector = tripleVectorToInsert;
        //this.pendingIntent = pendingIntent;
    }

	/*@Override
	public void run() {
        try {
            smartSpace = new KPICore(this.HOST, this.PORT, this.SPACE_NAME);
		    String joinResult = smartSpace.join();
            if (smartSpace.xmlTools.isJoinConfirmed(joinResult)) {
                String insertResult = smartSpace.insert(tripleVector);
                if (smartSpace.xmlTools.isInsertConfirmed(insertResult)) {
                    pendingIntent.send(SmartSpaceService.RESULT_SUCCESS_INSERT);
                    Log.d("SmartSpaceInsert "," Information has been inserted");
                } else {
                    pendingIntent.send(SmartSpaceService.RESULT_FAILURE_INSERT);
                    Log.d("SmartSpaceInsert "," Error, while inserting the information.");
                }
            } else {
                pendingIntent.send(SmartSpaceService.RESULT_FAILURE_JOIN);
            }
            smartSpace.leave();
        } catch (PendingIntent.CanceledException e){
            Log.e("SmartSpaceInsert. Pending Intent Error, Canceled", e.toString());
            e.printStackTrace();
        } catch (Exception e){
            Log.e("SmartSpaceInsert Error", e.toString() + "\n String to Insert" +tripleVector.toString());
            e.printStackTrace();
        }
	}
*/
    public boolean insert(Vector<Vector<String>> tripleVectorToInsert){
        boolean result = false;
        try {
            if (kp.isConnectionActive()) {
                String insertResult = smartSpace.insert(tripleVectorToInsert);
                if (smartSpace.xmlTools.isInsertConfirmed(insertResult)) {
                    result = true;
                } else {
                    result = false;
                }
            } else {
                result = false;
            }
        } catch (Exception e){
            Log.e("SmartSpaceInsert Error", e.toString() + "\n String to Insert" +tripleVectorToInsert.toString());
            e.printStackTrace();
        }
        return result;
    }

}
